package com.ossean.match.matchprocess;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MatchIncrement extends Thread implements Runnable {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private int prjHistory; // the record of project after matching last time
	private int memoHistory; // the record of memo after matching last time
	private int prjIndexed;  //the record of prj indexed to
	private int memoIndexed; //the record of memo indexed to
	private int currentProject; // current project of matching this time
	private int currentMemo; // current memo of matching this time
	private int currentPrjIndexed;
	private int currentMemoIndexed;
	private static File record; // record file "record.txt"
	private boolean newPrjComming;
	private boolean newMemoComming;
	private boolean currentMatchDone = true;
	private int lastPrjId = 0;
	private int lastTagId = 0;
	private int tagHistory = 0;
	private int currentTag = 0;
	private boolean newTagflag;
	private boolean newPrePrjFlag;
	private int prePrjHistory = 0;
	private int lastPrePrjId = 0;
	private int currentPrePrj = 0;
	private int taskToPrjId = 0;
	private int sleepTime = 0;
	private List<String>cityList;
	
	public MatchIncrement() {
		record = new File("record.txt");
		getMatchRecord();
		currentProject = prjHistory;
		currentMemo = memoHistory;
		currentTag = tagHistory;
		currentPrePrj = prePrjHistory;
		currentPrjIndexed = prjIndexed;
		currentMemoIndexed = memoIndexed;
		this.setCityList(initCityList());
	}
	
	public List<String> getCityList() {
		return cityList;
	}

	public void setCityList(List<String> cityList) {
		this.cityList = cityList;
	}

	public boolean isNewPrjComming() {
		return newPrjComming;
	}

	public void setNewPrjComming(boolean newPrjComming) {
		this.newPrjComming = newPrjComming;
	}

	public boolean isNewMemoComming() {
		return newMemoComming;
	}

	public void setNewMemoComming(boolean newMemoComming) {
		this.newMemoComming = newMemoComming;
	}

	public int getSleepTime() {
		return sleepTime;
	}

	public void setSleepTime(int sleepTime) {
		this.sleepTime = sleepTime;
	}

	public int getTaskToPrjId() {
		return taskToPrjId;
	}

	public void setTaskToPrjId(int taskToPrjId) {
		this.taskToPrjId = taskToPrjId;
	}

	public int getCurrentPrjIndexed() {
		return currentPrjIndexed;
	}

	public void setCurrentPrjIndexed(int currentPrjIndexed) {
		this.currentPrjIndexed = currentPrjIndexed;
	}

	public int getCurrentMemoIndexed() {
		return currentMemoIndexed;
	}

	public void setCurrentMemoIndexed(int currentMemoIndexed) {
		this.currentMemoIndexed = currentMemoIndexed;
	}

	public int getMemoIndexed() {
		return memoIndexed;
	}

	public void setMemoIndexed(int memoIndexed) {
		this.memoIndexed = memoIndexed;
	}

	public int getPrjIndexed() {
		return prjIndexed;
	}

	public void setPrjIndexed(int prjIndexed) {
		this.prjIndexed = prjIndexed;
	}

	public boolean isNewPrePrjFlag() {
		return newPrePrjFlag;
	}

	public void setNewPrePrjFlag(boolean newPrePrjFlag) {
		this.newPrePrjFlag = newPrePrjFlag;
	}

	public int getPrePrjHistory() {
		return prePrjHistory;
	}

	public void setPrePrjHistory(int prePrjHistory) {
		this.prePrjHistory = prePrjHistory;
	}

	public int getLastPrePrjId() {
		return lastPrePrjId;
	}

	public void setLastPrePrjId(int lastPrePrjId) {
		this.lastPrePrjId = lastPrePrjId;
	}

	public int getCurrentPrePrj() {
		return currentPrePrj;
	}

	public void setCurrentPrePrj(int currentPrePrj) {
		this.currentPrePrj = currentPrePrj;
	}

	public int getCurrentTag() {
		return currentTag;
	}

	public void setCurrentTag(int currentTag) {
		this.currentTag = currentTag;
	}

	public int getTagHistory() {
		return tagHistory;
	}

	public void setTagHistory(int tagHistory) {
		this.tagHistory = tagHistory;
	}

	public boolean isNewTagflag() {
		return newTagflag;
	}

	public void setNewTagflag(boolean newTagflag) {
		this.newTagflag = newTagflag;
	}

	public void setLastPrjId(int id) {
		this.lastPrjId = id;
	}

	public int getLastPrjId() {
		return lastPrjId;
	}

	public int getLastTagId() {
		return lastTagId;
	}

	public void setLastTagId(int id) {
		this.lastTagId = id;
	}

	public boolean isNewMemoTag() {
		return newTagflag;
	}

	public int getPrjHistory() {
		return prjHistory;
	}

	public synchronized void setPrjHistory(int prjHistory) {
		this.prjHistory = prjHistory;
	}

	public int getMemoHistory() {
		return memoHistory;
	}

	public synchronized void setMemoHistory(int memoHistory) {
		this.memoHistory = memoHistory;
	}

	public boolean isCurrentMatchDone() {
		return currentMatchDone;
	}

	public synchronized void setCurrentMatchDone(boolean currentMatchDone) {
		this.currentMatchDone = currentMatchDone;
	}

	public int getCurrentProject() {
		return currentProject;
	}

	public int getCurrentMemo() {
		return currentMemo;
	}

	public synchronized void setCurrentProject(int prjId) {
		currentProject = prjId;
	}

	public synchronized void setCurrentMemo(int memoId) {
		currentMemo = memoId;
	}

	/**
	 * read the record after matching last time from record file "record.txt",
	 * the format of the record in the file: "project's record" "memo's record"
	 */
	public void getMatchRecord() {
		try {
			Scanner in = new Scanner(record);
			this.setPrjHistory(in.nextInt());
			this.setMemoHistory(in.nextInt());
			this.setTagHistory(in.nextInt());
			this.setPrePrjHistory(in.nextInt());
			this.setPrjIndexed(in.nextInt());
			this.setMemoIndexed(in.nextInt());
			this.setTaskToPrjId(in.nextInt());
			in.close();
		} catch (FileNotFoundException e) {
			logger.error("getMatchRecord FileNotFoundException： " + e);
		}
	}
	
	public List<String> initCityList() {
		List<String> cityList = null;
		try {
			FileReader reader = new FileReader("city.txt");
			BufferedReader br = new BufferedReader(reader);
		    cityList = new ArrayList<String>();
		    String str = null;
		    while((str = br.readLine()) != null) {
		        cityList.add(str);
		    }
	        br.close();
	        reader.close();
		} catch (FileNotFoundException e) {
			logger.error("initCityList FileNotFoundException： " + e);
		} catch (IOException e) {
			logger.error("initCityList IOException： " + e);
		}
		return cityList;
	}

	/**
	 * write the result to the record file
	 */
	public void writeIntoRecord() {
		try {
			FileOutputStream fout = new FileOutputStream(record);
			fout.write((currentProject + "\t" + currentMemo + "\t" + currentTag
					+ "\t" + currentPrePrj + "\t" + currentPrjIndexed + "\t" + currentMemoIndexed + "\t" +  taskToPrjId).getBytes());
			fout.close();
		} catch (FileNotFoundException e) {
			logger.error("writeIntoRecord FileNotFoundException： " + e);
		} catch (IOException e) {
			logger.error("writeIntoRecord FileNotFoundException： " + e);
		}
	}
}